<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/albums' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>相册列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" align="center" width="80"></el-table-column>
      <el-table-column prop="name" label="名称" align="center" width="230"></el-table-column>
      <el-table-column prop="description" label="简介" header-align="center"></el-table-column>
      <el-table-column prop="sort" label="排序序号" align="center" width="100"></el-table-column>
      <el-table-column label="操作" align="center" width="100">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" circle
                     @click="handleEdit(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" circle
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="100%"
        :before-close="handleClose">
            <span>

              <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
                <el-form-item label="名称" prop="name">
                  <el-input v-model="ruleForm.name"></el-input>
                </el-form-item>
                <el-form-item label="简介" prop="description">
                 <el-input v-model="ruleForm.description"></el-input>
                </el-form-item>
                <el-form-item label="排序序号" prop="sort">
                  <el-input v-model="ruleForm.sort"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="submitForm(album)">修改</el-button>
                  <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
              </el-form>
            </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      dialogVisible: false,
      ruleForm: {
        name: '',
        description: '',
        sort: 0
      },
      rules: {
        name: [
          {required: true, message: '请输入相册名称', trigger: 'blur'},
          {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请输入相册简介', trigger: 'blur'},
          {min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入排序序号', trigger: 'blur'},
          {pattern: /^(\d{1}|[1-9]{1}[0-9]?)$/, message: '排序序号必须是 0~99 之间的数字', trigger: 'blur'}
        ]
      },
      album: {}
    };
  },
  methods: {
    openDeleteConfirm(album) {
      let title = '提示';
      let message = '此操作将永久删除【' + album.name + '】相册，是否继续？';
      this.$confirm(message, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(album);
      }).catch(() => {
      });
    },
    handleDelete(album) {
      console.log('handleDelete ... id=' + album.id);
      let url = 'http://localhost:8090/albums/' + album.id + '/delete';
      console.log('url = ' + url);
      this.axios.post(url).then((response) => {
        let responseBody = response.data;
        console.log(responseBody);
        if (responseBody.code != 2000) {
          this.$message.error(responseBody.message);
        }
        this.loadAlbumList();
      });
    },
    loadAlbumList() {
      console.log('loadAlbumList ...');
      let url = 'http://localhost:8090/albums';
      console.log('url = ' + url);
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.code == 2000) {
          this.tableData = responseBody.data;
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    handleClose(){
      this.dialogVisible=false;
    },
    handleEdit(album) {
      this.dialogVisible = true;
      this.album = album;
    },
    submitForm(album){
      this.$refs.ruleForm.validate(valid => {
        console.log(valid)
        if (valid === true) {
          // 调后台登陆接口
          let url = 'http://localhost:8090/albums/'+album.id+'/update';
          this.axios.post(url,this.ruleForm).then((response) => {
            let responseBody = response.data;
            if (responseBody.code != 2000) {
              this.$message.error(responseBody.message);
            }
            this.loadAlbumList();
            this.dialogVisible=false;
            this.$message({
              message: '修改成功！',
              type: 'success'
            });
          });
        }
      })


    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    console.log('mounted ...');
    this.loadAlbumList();
  },
  created() {
    console.log('created ...');
  }
}
</script>

<style scoped>

</style>